package api

import (
	"awesomeProject2/global"
	"awesomeProject2/model"
	"awesomeProject2/model/request"
	"awesomeProject2/model/response"
	"awesomeProject2/service"
	_ "fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"net/http"
	"strconv"
	"strings"
)

func CreatePaper(c *gin.Context) {
	var paper model.Paper
	var requestInfo request.Paper
	_ = c.ShouldBindJSON(&requestInfo)

	// 将request中的字段赋值到paper
	service.FillPaper(c, &paper, &requestInfo)

	if err := service.CreatePaper(&paper); err != nil {
		global.GVA_LOG.Error("新增Paper失败!", zap.Any("err", err))
		response.FailWithError(err, c)
	} else {
		response.Ok(c)
	}
}

func GetPaper(c *gin.Context) {
	dataId, _ := strconv.Atoi(c.Param("id"))
	if err, repaper := model.GetPaper(uint(dataId)); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":    "查询失败",
			"status": http.StatusNotFound,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data": repaper,
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			}},
		)
	}
}

// GetPapers 根据userid查询论文列表
func GetPapers(c *gin.Context) {
	pageSize, _ := strconv.Atoi(c.Query("pagesize"))
	pageNum, _ := strconv.Atoi(c.Query("pagenum"))
	userid, _ := strconv.Atoi(c.Query("userid"))
	if pageSize == 0 {
		pageSize = -1
	}
	if pageNum == 0 {
		pageNum = 1
	}
	if err, paper, total := model.GetPapers(uint(userid), pageSize, pageNum); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "查询失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"data":  gin.H{"paperList": paper},
			"total": total,
			"meta":  gin.H{"msg": "查询成功", "status": http.StatusOK},
		})
	}
}
func UpdatePaper(c *gin.Context) {
	var data request.Paper
	_ = c.ShouldBindJSON(&data)
	var paper model.Paper
	paper = model.Paper{
		PaperName:     strings.TrimSpace(data.PaperName),
		MagazineName:  data.MagazineName,
		PaperRole:     strings.Join(data.PaperRoleList, ","),
		PaperCategory: data.PaperCategory,
		PaperAuthors:  data.PaperAuthors,
		PaperLevels:   strings.Join(data.PaperLevelList, ","),
		PaperFile:     strings.Join(data.PaperFileList, ","),
		Year:          data.Year,
	}
	paper.ID = data.ID
	if err := model.UpdatePaper(paper); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "更新失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{"meta": gin.H{"msg": "更新成功", "status": http.StatusOK}})
	}
}
func DeletePaper(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := model.DeletePaper(id); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "删除失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{"meta": gin.H{"msg": "删除成功", "status": http.StatusOK}})
	}
}

func ChangePaperVisiblity(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	if err := model.ChangePaperVisiblity(uint(id)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": "更新失败", "status": http.StatusNotFound})
	} else {
		c.JSON(http.StatusOK, gin.H{"meta": gin.H{"msg": "更新成功", "status": http.StatusOK}})
	}
}

func GetAllPapers(c *gin.Context) {
	userid, _ := c.Get("userid")
	year, _ := strconv.Atoi(c.Param("year"))
	if err, data, tt := model.GetAllPapers(userid.(uint), year); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": userid.(uint), "status": http.StatusNotFound})
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"paperList": data, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}

func GetPapersDynamic(c *gin.Context) {
	var searchinfo request.PaperSearchInfo
	_ = c.ShouldBindQuery(&searchinfo)
	if err, list, total := service.GetPapersDynamic(searchinfo); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.Fail(c)
	} else {
		response.OkWithData(response.PageResult{
			Total: total,
			List:  list,
		}, c)
	}
}

func GetAllPaperWithoutYear(c *gin.Context) {
	userid, _ := c.Get("userid")
	if err, data, tt := model.GetAllPapers3(userid.(uint)); err != nil {
		c.JSON(http.StatusOK, gin.H{"msg": userid.(uint), "status": http.StatusNotFound})
	} else {

		c.JSON(http.StatusOK, gin.H{
			"data": gin.H{"paperList": data, "total": tt},
			"meta": gin.H{
				"msg":    "查询成功",
				"status": http.StatusOK,
			},
		})
	}
}
